#include "StdAfx.h"
//-------------------------------------------------------------------------------------------------
//EllipseTool.cpp : Tool used to draw Image's Ellipse type Image Objects   
//-------------------------------------------------------------------------------------------------
//FreeBSD License
//Copyright 2011 Will Stott All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are
//permitted provided that the following conditions are met:
//
//   1. Redistributions of source code must retain the above copyright notice, this list of
//      conditions and the following disclaimer.
//
//   2. Redistributions in binary form must reproduce the above copyright notice, this list
//      of conditions and the following disclaimer in the documentation and/or other materials
//      provided with the distribution.
//
//THIS SOFTWARE IS PROVIDED BY Will Stott ``AS IS'' AND ANY EXPRESS OR IMPLIED
//WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
//FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Will Stott OR
//CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
//CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
//ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
//ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//The views and conclusions contained in the software and documentation are those of the
//authors and should not be interpreted as representing official policies, either expressed
//or implied, of Will Stott.
//
//----Authors--------------------------------------------------------------------------------------
//created by: will.stott@ucl.ac.uk July 2011
//
//----Comments-------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------
#include "EllipseTool.h"
#include "EllipseShape.h"


EllipseTool::EllipseTool(void)
{
	SetImageObjectType('\0');
}

EllipseTool::EllipseTool(ImageObjectType *pImageObjectType)
{
	SetImageObjectType(pImageObjectType);
}

EllipseTool::~EllipseTool(void)
{

}

int		EllipseTool::InsertDynMenuItem(void* pMenuPopup, int iMenuIndex)
{
	int rc = -1;

	CMFCPopupMenu* pPopup = reinterpret_cast<CMFCPopupMenu*>(pMenuPopup);
	if (pPopup)
	{
		CString menuText(GetImageObjectType()->GetMenuName());
		CMFCToolBarMenuButton button1(GetImageObjectType()->GetResourceId(), NULL, -1, menuText);
		rc = pPopup->InsertItem (button1, iMenuIndex);
		SetMenuIndex(rc);
	}
	return rc;
}

bool	EllipseTool::DrawImageDynMenu(void* pDC, const void* pMenuButton, ImageRect& menuIconImage, bool selected)
{

	bool rc = false;

	CDC* pDevContext = reinterpret_cast<CDC*>(pDC);
	const CMFCToolBarMenuButton* pButton = reinterpret_cast<const CMFCToolBarMenuButton*>(pMenuButton);
	if (pButton && pDevContext)
	{
		if (GetResourceId() == pButton->m_nID)
		{
			CBrush br (GetImageObjectType()->GetPenColour());

			CRect rect(menuIconImage.GetTopLeft().x, menuIconImage.GetTopLeft().y, menuIconImage.GetBotRight().x, menuIconImage.GetBotRight().y);
			rect.DeflateRect (2, 2);

			pDevContext->FillRect (rect, &br);

			if (selected)
			{
				CBrush dot (GetImageObjectType()->GetInversePenColour());
				rect.DeflateRect (5, 5);
				pDevContext->FillRect (rect, &dot);
			}
			rc = true;
		}
	}
	return rc;
}


#pragma comment(lib, "gdiplus.lib")


void	EllipseTool::DrawImageObject(void *pGraphics, ImageObject *pImageObject, bool selected)
{
	using namespace Gdiplus;

	Graphics *pGdiGraphics = reinterpret_cast<Graphics *>(pGraphics);
	EllipseShape *pShape = dynamic_cast<EllipseShape *>(pImageObject);

	if ((pShape != '\0' ) && (pGdiGraphics != '\0'))
	{
		COLORREF penColour(pShape->GetImageObjectType()->GetPenColour());		//you can also get this info from the tool itself GetImageObjectType()->GetPenColour();
		Pen* myPen = new Pen(Color(GetRValue(penColour),GetGValue(penColour),GetBValue(penColour)), (selected) ? 3.0F : 1.0F); 

		ImageRect BoundingRect(pShape->GetContainingRect());

		pGdiGraphics->DrawEllipse(myPen, BoundingRect.GetTopLeft().x, BoundingRect.GetTopLeft().y, BoundingRect.GetWidth(), BoundingRect.GetHeight());
	}
}

ImageObject *EllipseTool::DoLButtonDown(UINT nFlags, const ImagePt& point, ImageProps *pImageProps) 
{
	ImageObject *rc = '\0';

	ImageObjectType *pImageObjType = GetImageObjectType();				//get the type of image object for this tool

	if ((rc = pImageProps->CreateImageObject (pImageObjType)) != '\0')
	{
		EllipseShape *pShape = dynamic_cast<EllipseShape *>(rc);
		if (pShape != '\0')
		{
			pShape->StartBoundary(point.x, point.y);
			if (pImageProps->SetSelImageObject(pShape) == false)		
				pShape = '\0';
		}
		if (pShape == '\0')
		{
			pImageProps->DeleteImageObject (rc);
			rc = '\0';
		}
	}
	return rc;
}


bool	EllipseTool::DoLButtonUp(UINT nFlags, const ImagePt& point, ImageObject *pSelImageObject)
{
	bool rc = false;

	EllipseShape *pShape =	dynamic_cast<EllipseShape *>(pSelImageObject);
	if ( pShape != '\0' )
	{
		pShape->CloseBoundary(point.x, point.y);		
		rc = true;
	}
	return rc;
}

bool	EllipseTool::DoMouseMove(UINT nFlags, const ImagePt& point, ImageObject *pSelImageObject)
{
	bool rc = false;

	EllipseShape *pShape =	dynamic_cast<EllipseShape *>(pSelImageObject);
	if ( pShape != '\0' )
	{
		pShape->ExtendBoundary(point.x, point.y);		
		rc = true;
	}
	return rc;
}


bool	EllipseTool::IsCompatible(ImageObject *pImageObject)
{
	bool rc = false;

	if (pImageObject != '\0')
	{
		std::string baseClass (pImageObject->GetImageObjectType()->GetBaseClassName());
		if (   baseClass == EllipseShapeType::baseClassName )
			rc = true;
	}
	return rc;
}

//-------------------------------------------------------------------------------------------------
//			E N D   O F   F I L E
//-------------------------------------------------------------------------------------------------
